package com.everysoft.autoanswer;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class ScreenButtonActivity extends Activity {

	private Intent service_intent;
	private static boolean service_running = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		service_intent = new Intent(this, AutoAnswerIntentService.class);
		super.onCreate(savedInstanceState);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_SPLIT_TOUCH);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.on_call);
		registerReceiver();

		((Button) this.findViewById(R.id.screen)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				startActivity(new Intent(ScreenButtonActivity.this, OnCallButtonActivity.class));
				startService(service_intent);
			}
		});

	}

	@Override
	protected void onResume() {
		service_intent = new Intent(this, AutoAnswerIntentService.class);
		registerReceiver();

		super.onResume();
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			finish();

		}
	};

	@Override
	public void finish() {
		unregisterReceiver(receiver);
		super.finish();
	};

	private void registerReceiver() {
		IntentFilter filter = new IntentFilter();

		filter.addAction("com.finish.ACTIVITY");
		registerReceiver(receiver, filter);
	}

}